/*
 *   Copyright 2023 <a href="mailto:asialjim@hotmail.com">Asial Jim</a>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.asialjim.encommunicate.base.rsa;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * 基于 RSA 算法的签名器
 *
 * @author Copyright © <a href="mailto:asialjim@hotmail.com">Asial Jim</a>   Co., LTD
 * @version 1.0
 * @since 2023/2/24, &nbsp;&nbsp; <em>version:1.0</em>, &nbsp;&nbsp; <em>java version:17</em>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SimpleRSASigner implements RSASigner{
    private RSAKeyPairProperty pairProperty;

    @Override
    public String sign(String source) {
        return sign(source, pairProperty.getPriKey());
    }

    @Override
    public String sign(byte[] source) {
        return sign(source, pairProperty.getPriKey());
    }

    @Override
    public boolean checkSign(String source, String signature) {
        return checkSign(source, pairProperty.getPubKey(),signature);
    }

    @Override
    public boolean checkSign(byte[] source, String signature) {
        return checkSign(source, pairProperty.getPubKey(),signature);
    }

    @Override
    public boolean checkSign(InputStream inputStream, String signature) {
        try {
            return checkSign(IOUtils.toByteArray(inputStream), signature);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}